package org.iabako.shared.entity.business;

import com.google.gwt.user.client.rpc.GwtTransient;
import org.iabako.shared.entity.enumeration.CustomFieldType;
import org.iabako.shared.entity.user.Enterprise;
import org.iabako.shared.tools.GenericTools;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jose on 17/09/14.
 */

@Entity
@Table(name = "CUSTOM_FIELD")
public class CustomField extends AbstractEntity {

    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id;

    @Column(name = "TITLE", nullable = false)
    @NotNull
    private String title;

    @Column(name = "TYPE", nullable = false)
    @Enumerated(EnumType.STRING)
    @NotNull
    private CustomFieldType customFieldType;

    @Column(name = "CRITERIA_SEARCH", nullable = false)
    @NotNull
    private boolean criteriaSearch;

    @Column(name = "POSITION", nullable = false)
    @NotNull
    private int position;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_ENTERPRISE", nullable = false)
    @GwtTransient
    private Enterprise enterprise;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "customField")
    @OrderBy("label")
    //To Avoid serializing exception in lazy/proxy objects.
    @GwtTransient
    private List<CustomFieldListItem> customFieldListItems;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "customField")
    //To Avoid serializing exception in lazy/proxy objects.
    @GwtTransient
    private List<CustomFieldValue> customFieldValues;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public CustomFieldType getCustomFieldType() {
        return customFieldType;
    }

    public void setCustomFieldType(CustomFieldType customFieldType) {
        this.customFieldType = customFieldType;
    }

    public boolean isCriteriaSearch() {
        return criteriaSearch;
    }

    public void setCriteriaSearch(boolean criteriaSearch) {
        this.criteriaSearch = criteriaSearch;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public List<CustomFieldListItem> getCustomFieldListItems() {
        if (customFieldListItems == null) {
            customFieldListItems = new ArrayList<CustomFieldListItem>();
        }
        return customFieldListItems;
    }

    public void setCustomFieldListItems(List<CustomFieldListItem> customFieldListItems) {
        this.customFieldListItems = customFieldListItems;
    }

    public List<CustomFieldValue> getCustomFieldValues() {
        return customFieldValues;
    }

    public void setCustomFieldValues(List<CustomFieldValue> customFieldValues) {
        this.customFieldValues = customFieldValues;
    }

    @Transient
    public Map<Long, String> getCustomFieldListItemsAsMap() {
        Map<Long, String> items = new LinkedHashMap<Long, String>();

        if (GenericTools.isEmpty(getCustomFieldListItems())) {
            return items;
        }

        for (CustomFieldListItem item : getCustomFieldListItems()) {
            items.put(item.getId(), item.getLabel());
        }
        return items;
    }

    @Transient
    @Override
    public void copyDatesAsString() {
        return;
    }
}
